using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Infragistics.Windows.DataPresenter;

using ViewOnCore.Helper.WPF;
using ViewOnCore.HostContract;
using ViewOnCore.HostContract.Media;

namespace UI.Control
{
    public partial class KioskThumbnail : System.Windows.Controls.UserControl
    {

        #region Variables

        public static readonly DependencyProperty ItemProperty =
            DependencyProperty.Register("Item",
                                        typeof(AlbumCoverItem),
                                        typeof(KioskThumbnail),
                                        new PropertyMetadata(null, new PropertyChangedCallback(item_changed)));

        private Kiosk _kiosk;

        #endregion

        #region Constructor

        public KioskThumbnail(Kiosk kiosk)
        {
            InitializeComponent();

            _kiosk = kiosk;

            this.MouseEnter += new MouseEventHandler(KioskThumbnail_MouseEnter);
            this.MouseLeave += new MouseEventHandler(KioskThumbnail_MouseLeave);

            //btnOpenlbum.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(btnOpenlbum_PreviewMouseLeftButtonDown);
        }

        #endregion

        #region Properties

        public AlbumCoverItem Item
        {
            get
            {
                return (AlbumCoverItem)GetValue(ItemProperty);
            }
            set
            {
                SetValue(ItemProperty, value);
            }
        }

        #endregion

        #region Events

        static private void item_changed(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue == null)
                return;

            if (!ViewOnCore.Visual.VisualPartHelper.IsCurrentMediaDeviceSupported("Music"))
                return;

            KioskThumbnail thumbnail = sender as KioskThumbnail;
            AlbumCoverItem item = args.NewValue as AlbumCoverItem;

            item.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(thumbnail.item_PropertyChanged);

            thumbnail.UpdateContent();
        }

        void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (!ViewOnCore.Visual.VisualPartHelper.IsCurrentMediaDeviceSupported("Music"))
                return;

            if (e.PropertyName != "BitmapSource")
                return;

            // Refresh the image
            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                (DelegateInvoker)delegate { UpdateContent(); });
        }

        void btnOpenlbum_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OpenAlbumDialog();
        }

        void KioskThumbnail_MouseLeave(object sender, MouseEventArgs e)
        {
            _kiosk.OnMouseLeaveItem(Item);
        }

        void KioskThumbnail_MouseEnter(object sender, MouseEventArgs e)
        {
            _kiosk.OnMouseEnterItem(Item);
        }

        #endregion

        #region UpdateContent

        private void UpdateContent()
        {
            if (Item == null)
                return;

            //lblTitle1.Text = Item.Title01;
            //lblTitle2.Text = Item.Title02;
            imgThumbail.Source = Item.BitmapSource;
        }

        #endregion

        #region OpenAlbumDialog

        internal void OpenAlbumDialog()
        {
            _kiosk.OnAlbumDialogOpening();

            KioskAlbumDialog dialog = new KioskAlbumDialog(Item);
            dialog.ShowDialog((Desktop)Host.ApplicationDesktop);
        }

        #endregion

    }
}